# -*- coding: utf-8 -*-
"""
Created on Mon Oct 14 20:12:50 2024

@author: 34101
"""

import os
import uuid
# 需要安装xlwt包
import xlwt


def generate_uuids(num_uuids):
    uuids = []
    for _ in range(num_uuids):
        uuids.append(str(uuid.uuid4()))
    return uuids

def save_to_excel(uuids, filename):
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('UUIDs')
    
    # 添加列标题
    worksheet.write(0, 0, 'FID')
    worksheet.write(0, 1, 'uuid')

    # 写入数据
    for i, (fid, uuid_str) in enumerate(zip(range(len(uuids)), uuids), start=1):
        worksheet.write(i, 0, fid)
        worksheet.write(i, 1, uuid_str)
    
    workbook.save(filename)


if __name__ == "__main__":
    try:
        # 指定要生成的 UUID 数量
        # num_uuids = int(input("请输入你想要的uuid个数："))
        num_uuids = 3
    except:
        print("输入内容不为数值！")
    
    else:
        
        # 生成 UUIDs
        uuids = generate_uuids(num_uuids)
    
        # 将 UUIDs 保存到 Excel 文件
        # 自己指定文件保存路径及名称
        try:
            # 获取桌面路径
            # desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
            desktop_path = "D:\\Temp\\GeneUUID"
            
        except:
            print("\033[0;31;40m","桌面路径获取失败！","\033[0m")
        
        else:
            
            # file_name = input("请输入存储uuid的文件名称，不带文件类型后缀\n")
            file_name = "梅浦社区"
            save_file = desktop_path+"\\"+file_name+".xls"
            save_to_excel(uuids, save_file)
            
            # 将 UUIDs 保存到 Excel 文件
            #save_to_excel(uuids, os.path.join(desktop_path, 'uuids_with_fid.xls'))
            
            print(f"{num_uuids} 个 UUIDs 已保存到 {save_file} 文件中。")
            
        finally:
            
            print("Finished!")
            # end = input("按下回车键1退出命令行...")
            